<?php
/**
 * @TODO Fill Header
 *
 * @author Xavier VILAIN
 * @package 
 * @subpackage 
 */
use Fox\Generator;
use Fox\Html\TextArea;
use Doctrine\DBAL\Connection;
use Fox\Common\Customer;

$xview = Xview::instance();
$values = null;
// Init Search Help
$o_shlp = new Fox\Html\SearchHelp("Customer_Competitor");
$o_shlp->setAction(null);
$excluded = array();
$o_shlp->getSelBlock()->add(new TextArea("excluded"))->hidden();
// Load already selected Product
$blItems = Xview::instance()->component("inquiry", "competitors"); 
$data = $blItems->item("gCompetitors")->get(null,true);
if(is_array($data))
{
	foreach($data as $line)
	{
		if(isset( $line['customerid']))
			$ids[] = $line['customerid'];
	}
	if(isset($ids))
	{
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($ids),array(Connection::PARAM_INT_ARRAY));
		$json = addslashes(json_encode($ids));
		$values = $json;
		$o_shlp->getSelBlock()->item("excluded")->default = $json;
	}
	
}
else
{
	$values = Xview::instance()->val("excluded");

	if(!empty($values))
	{
		$productId = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment("customerid NOT IN (?) ",array($productId),array(Connection::PARAM_STR_ARRAY));
	}
}

$o_shlp->grid()->fieldcat()->field("customer_code")->SetHidden();
$o_shlp->grid()->setCoche(true);
//$o_shlp->grid()->setLineCheck();
$o_shlp->grid()->footer()->add(new Fox\Html\Button('select',"hide('sh:Customer_Competitor');selectPartners('GRCustomer_Competitor');"));
$o_shlp->grid()->footer()->add(new Fox\Html\Button('new',Generator::display("newCustomer",
														  Fox\Generator::url("customer", "add","light",
														  				array("type" => Customer::TYPE_COMPETITOR
														  					 ))
														  )));
$force = true;
if(isset($_POST['dbcnt']) || $force)
{
	$block = $o_shlp->getSelBlock();
	$fragments[] = $block->item("competitor_name")->fragments("t.name1");
	$fragments[] = new Fox\Dao\SqlFragment("type = ?",array(Customer::TYPE_COMPETITOR));
	$fragments[] = $excluded;
	$finder = new Customer();
	$customers = $finder->find(Fox\Dao\SqlFragment::combine($fragments));
	$data = array();
	foreach($customers as $customer)
	{
		$data[] = array("competitor_name" => $customer->name1,"customer_code" => $customer->getId());
	}
	if(isset($_POST['dbcnt']) )
	{
		$o_shlp->DisplayGrid($data);
	}
	else
	{
		$o_shlp->getSelBlock()->collapsed(true);
		$o_shlp->setData($data);
		echo $o_shlp->Display();
	}
}
else
{
	echo $o_shlp->Display();
}